package com.itheima.dao;

import com.itheima.bean.pojo.User;
import com.itheima.bean.vo.AddUser;
import com.itheima.bean.vo.UpdateUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {

    /**
     * 添加用户数据
     *
     * @param user
     * @return
     */
    @Insert("insert into t_user(username,password,remark,email,createTime) " +
            "values (#{username},#{password},#{remark},#{email},now())")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    public int addUser(AddUser user);

    /**
     * 添加用户角色
     *
     * @param userid
     * @param roleid
     * @return
     */
    @Insert("insert into t_user_role(user_id, role_id) values (#{userid},#{roleid})")
    public int addUserRole(@Param("userid") Integer userid,
                           @Param("roleid") Integer roleid);


    //----------------------------------------------------------------------------------------------------


    //更新用户信息
    @Update("update t_user set username=#{username},email=#{email},password=#{password},remark=#{remark} where id=#{id}")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int updateUser(UpdateUser user);


    @Update("update t_user_role set user_id=#{userid},role_id=#{roleid}")
    public int updateUserRole(@Param("userid") Integer userid,
                              @Param("roleid") Integer roleid);


    //----------------------------------------------------------------------------------------------------

    /**
     * 分页查询： 用户信息及用户角色信息
     *
     * @param index
     * @param pageSize
     * @return
     */
    public List<User> findUserByPage(@Param("index") Integer index,
                                     @Param("pageSize") Integer pageSize);

    /**
     * 分页查询：统计查询总记录数
     *
     * @return
     */
    @Select("select count(*) from t_user")
    public int findCount();


}
